package com.smart_campus.teg.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.domain.User;
import com.smart_campus.teg.common.TEGResultObj;
import com.smart_campus.teg.domain.TegClass;
import com.smart_campus.teg.vo.DormitoryVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.smart_campus.teg.mapper.TegDormitoryMapper;
import com.smart_campus.teg.domain.TegDormitory;
import com.smart_campus.teg.service.TegDormitoryService;

import java.util.List;

@Service
public class TegDormitoryServiceImpl implements TegDormitoryService{

    @Resource
    private TegDormitoryMapper tegDormitoryMapper;

    //@CacheEvict 删除的注解通过对象id在redis中删除数据
//    @CacheEvict(cacheNames = "com.smart_campus.teg.service.impl.TegDormitoryServiceImpl",key = "#id")
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return tegDormitoryMapper.deleteByPrimaryKey(id);
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
//    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.TegDormitoryServiceImpl",key = "#result.id")
    @Override
    public TegDormitory insert(TegDormitory record) {
        tegDormitoryMapper.insert(record);
        return record;
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
//    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.TegDormitoryServiceImpl",key = "#result.id")
    @Override
    public TegDormitory insertSelective(TegDormitory record) {
        tegDormitoryMapper.insertSelective(record);
        return record;
    }

    //@Cacheable 返回对象 通过对象id在redis中创建对象保存,方便下次使用
//    @Cacheable(cacheNames ="com.smart_campus.teg.service.impl.TegDormitoryServiceImpl",key = "#id")
    @Override
    public TegDormitory selectByPrimaryKey(Integer id) {
        return tegDormitoryMapper.selectByPrimaryKey(id);
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
//    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.TegDormitoryServiceImpl",key = "#result.id")
    @Override
    public TegDormitory updateByPrimaryKeySelective(TegDormitory record) {
        tegDormitoryMapper.updateByPrimaryKeySelective(record);
        return record;
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
//    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.TegDormitoryServiceImpl",key = "#result.id")
    @Override
    public TegDormitory updateByPrimaryKey(TegDormitory record) {
        tegDormitoryMapper.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public DataGridView selDorimtory(DormitoryVo vo) {
        Page<Object> page = PageHelper.startPage(vo.getPage(),vo.getLimit());
        List<TegDormitory> list = tegDormitoryMapper.selDorimtory(vo);
        return new DataGridView(page.getTotal(),list);
    }

    @Override
    public Boolean updatedormitoryStaff(TegDormitory vo) {
        return tegDormitoryMapper.updatedormitoryStaff(vo);
    }

    @Override
    public Integer selBydormitoryNumber(TegDormitory tegDormitory) {
        return tegDormitoryMapper.selBydormitoryNumber(tegDormitory);
    }

    //@CachePut 要返回对象 添加通过reids主键回填的id来在redis创建对应id的对象数据
//    @CachePut(cacheNames = "com.smart_campus.teg.service.impl.TegDormitoryServiceImpl",key = "#result.id")
    @Override
    public TegDormitory selupdateBydormitoryNumber(TegDormitory tegDormitory) {
        return tegDormitoryMapper.selupdateBydormitoryNumber(tegDormitory);
    }

    @Override
    public Boolean existsStudent(TegDormitory tegDormitory) {
        User user = tegDormitoryMapper.selByStudengName(tegDormitory.getStudentName());//查询学生
        if (user==null){
            return false;
        }
        TegClass tegClass = tegDormitoryMapper.selByClassName(tegDormitory.getStudentClass());//查询宿舍
        if (tegClass==null){
            return false;
        }
        return tegDormitoryMapper.selByStuIDAndClassId(tegClass.getClassId(),user.getUserId());
    }

    @Override
    public User selByStudengName(String name) {
        return tegDormitoryMapper.selByStudengName(name);
    }


}
